package org.springframework.prospring.ticket.web.controllers;


import java.util.ArrayList;
import java.util.List;
import java.util.*;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import org.springframework.prospring.ticket.domain.location.Region;
import org.springframework.prospring.ticket.domain.Club;
import org.springframework.prospring.ticket.web.views.JsonView;
import org.springframework.prospring.ticket.service.RegionManager;
import org.springframework.prospring.ticket.service.ClubManager;
import org.springframework.prospring.ticket.service.CityManager;
import org.springframework.prospring.ticket.service.ProvinceManager;

@Controller
public class RegionSelectorController {
    ProvinceManager provinceManager;
    RegionManager regionManager;
    ClubManager clubManager;
    CityManager cityManager;

    @RequestMapping("/carselector.dahtml")
    public ModelAndView index() {
	ModelAndView mav = new ModelAndView("carselector");
	mav.addObject("makeList", getMakeList());
        System.out.println("=========== com.chariotsolutions.cardemo.web.CarSelectorController  ModelAndView = " + mav);
    return mav;
    }



    @RequestMapping("/findCityByProvince.dahtml")
    public ModelAndView findCityByProvince(@RequestParam("provinceId") Long provinceId) {
        System.out.println("==============RegionSelectorController.findCityByProvince provinceId="+provinceId);

        //Collection regions = this.regionManager.findAllRegions();
        //System.out.println("regions = " + regions);

        Collection cities = this.provinceManager.findCitiesByProvince(provinceId);
        System.out.println("cities = " + cities);

        ModelAndView mav = new ModelAndView(JsonView.instance);
    	mav.addObject(JsonView.JSON_ROOT, cities);
        //mav.addObject(JsonView.JSON_ROOT, regions);

        return mav;
    }

    //no se si se usara hay q cambiar el nombre regionByCity
    @RequestMapping("/findRegionByCity.dahtml")
    public ModelAndView findRegionByCity(@RequestParam("cityId") Long cityId) {
        System.out.println("==============RegionSelectorController.findRegionByCity cityId="+cityId);

        Collection regions = this.cityManager.findRegionsByCity(cityId);
        System.out.println("regions = " + regions);

        ModelAndView mav = new ModelAndView(JsonView.instance);
    	mav.addObject(JsonView.JSON_ROOT, regions);
    	return mav;
    }

    @RequestMapping("/findClubsById.dahtml")
    public ModelAndView findClubsById(@RequestParam("regionId") Long regionId) {
        System.out.println("==============RegionSelectorController.findClubsById regionId="+regionId);
        Collection clubs = this.clubManager.findAllClubsByRegionId(regionId);

        ModelAndView mav = new ModelAndView(JsonView.instance);
    	mav.addObject(JsonView.JSON_ROOT, clubs);
    	return mav;
    }

    @RequestMapping("/findClubsForAdvancedFilter.dahtml")
    public ModelAndView findClubsForAdvancedFilter(@RequestParam("provinceIdSelect") Long provinceIdSelect, @RequestParam("cityIdSelect") Long cityIdSelect, @RequestParam("regionIdSelect") Long regionIdSelect, @RequestParam("deporteIdSelect") Long deporteIdSelect) {
        System.out.println("==============RegionSelectorController.findClubsForAdvancedFilter provinceIdSelect="+provinceIdSelect+", cityIdSelect="+cityIdSelect+", deporteIdSelect="+deporteIdSelect);
        Collection clubs = this.clubManager.findAllClubsByRegionId(regionIdSelect);

        ModelAndView mav = new ModelAndView(JsonView.instance);
    	mav.addObject(JsonView.JSON_ROOT, clubs);
    	return mav;
    }    

    @RequestMapping("/models.dahtml")
    public ModelAndView models(@RequestParam("makeId") Long makeId) {
        System.out.println("============== RegionSelectorController.models makeId="+makeId);
        
        ModelAndView mav = new ModelAndView(JsonView.instance);
    	mav.addObject(JsonView.JSON_ROOT, getModelList(makeId));
    	return mav;
    }

    @RequestMapping("/years.dahtml")
    public ModelAndView years(@RequestParam("modelId") Long modelId) {
    	ModelAndView mav = new ModelAndView(JsonView.instance);
    	mav.addObject(JsonView.JSON_ROOT, getYearList(modelId));
    	return mav;
    }

    private List<Region> getMakeList() {
	// hard code some test data
	List<Region> makeList = new ArrayList<Region>();
	makeList.add(new Region((long) 1, "CAB", "Caballito"));
	makeList.add(new Region((long) 2, "PAL", "Palermo"));
	makeList.add(new Region((long) 3, "RET", "Retiro"));
	return makeList;
    }

    private List<Region> getModelList(Long makeId) {
	return getMakeList();
    }

    private List<Region> getYearList(Long modelId) {
	return getMakeList();
    }


    public ProvinceManager getProvinceManager() {
        System.out.println("================ RegionSelectorController.getProvinceManager");
        return provinceManager;
    }

    public void setProvinceManager(ProvinceManager provinceManager) {
        System.out.println("================ RegionSelectorController.setProvinceManager");
        this.provinceManager = provinceManager;
    }

    public RegionManager getRegionManager() {
        System.out.println("===================== RegionSelectorController.getRegionManager");
        return regionManager;
    }

    public void setRegionManager(RegionManager regionManager) {
        System.out.println("===================== RegionSelectorController.setRegionManager");
        this.regionManager = regionManager;
    }


    public ClubManager getClubManager() {
        System.out.println("===================== RegionSelectorController.getClubManager");
        return clubManager;
    }

    public void setClubManager(ClubManager clubManager) {
        System.out.println("===================== RegionSelectorController.setClubManager");
        this.clubManager = clubManager;
    }


    public CityManager getCityManager() {
        System.out.println("============= RegionSelectorController.getCityManager");
        return cityManager;
    }

    public void setCityManager(CityManager cityManager) {
        System.out.println("================    RegionSelectorController.setCityManager");
        this.cityManager = cityManager;
    }
}
